Kinetis SDK Demo Applications User Guide  1.0.0-beta
Freescale Semiconductor, Inc.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
low_power_demo.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 - 2014, Freescale Semiconductor, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * o Redistributions of source code must retain the above copyright notice, this list
9  * of conditions and the following disclaimer.
10  *
11  * o Redistributions in binary form must reproduce the above copyright notice, this
12  * list of conditions and the following disclaimer in the documentation and/or
13  * other materials provided with the distribution.
14  *
15  * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from this
17  * software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 #ifndef __LOW_POWER_DEMO_H__
31 #define __LOW_POWER_DEMO_H__
32 
33 #include <stdint.h>
34 #include "uart/fsl_uart_driver.h"
35 #include "smc/fsl_smc_manager.h"
36 #include "fsl_mcg_hal.h"
37 
38 /*******************************************************************************
39  * Defination
40  ******************************************************************************/
41 #define LOW_POWER 0
42 #define HIGH_GAIN 1
44 #define CLK0_TYPE CRYSTAL
46 #define MCGOUT 1
48 #define SWD_CLK_DISABLE port_hal_mux_control(HW_PORTA, 0, kPortPinDisabled)
49 #define SWD_DIO_DISABLE port_hal_mux_control(HW_PORTA, 3, kPortPinDisabled)
51 #define SWD_CLK_ENABLE port_hal_mux_control(HW_PORTA, 0, kPortMuxAlt7)
52 #define SWD_DIO_ENABLE port_hal_mux_control(HW_PORTA, 3, kPortMuxAlt7)
55 #if defined(FRDM_KL25Z48M)
56 /* LED defines */
57 //#define LED0_EN (port_hal_mux_control(HW_PORTB, 18, kPortMuxAsGpio)) /*!< Enable target LED0 */
58 //#define LED1_EN (port_hal_mux_control(HW_PORTB, 18, kPortMuxAsGpio)) /*!< Enable target LED1 */
59 //#define LED2_EN (port_hal_mux_control(HW_PORTD, 1, kPortMuxAsGpio)) /*!< Enable target LED2 */
60 
61 //#define LED0_OFF (gpio_write_pin_output(rgb_led_pins[0].pinName, 1)) /*!< Turn off target LED0 */
62 //#define LED1_OFF (gpio_write_pin_output(rgb_led_pins[1].pinName, 1)) /*!< Turn off target LED1 */
63 //#define LED2_OFF (gpio_write_pin_output(rgb_led_pins[2].pinName, 1)) /*!< Turn off target LED2 */
64 
65 //#define LED0_ON (gpio_write_pin_output(rgb_led_pins[0].pinName, 0)) /*!< Turn on target LED0 */
66 //#define LED1_ON (gpio_write_pin_output(rgb_led_pins[1].pinName, 0)) /*!< Turn on target LED1 */
67 //#define LED2_ON (gpio_write_pin_output(rgb_led_pins[2].pinName, 0)) /*!< Turn on target LED2 */
68 
69 //#elif defined(TWR_K22F120M)
70 #else
71 /* LED defines */
72 #define LED0_EN (port_hal_mux_control(HW_GPIOD, 4, kPortMuxAsGpio))
73 #define LED1_EN (port_hal_mux_control(HW_GPIOD, 5, kPortMuxAsGpio))
74 #define LED2_EN (port_hal_mux_control(HW_GPIOD, 6, kPortMuxAsGpio))
75 #define LED3_EN (port_hal_mux_control(HW_GPIOD, 7, kPortMuxAsGpio))
77 #define LED0_OFF (gpio_write_pin_output(ledPins[0].pinName, 1))
78 #define LED1_OFF (gpio_write_pin_output(ledPins[1].pinName, 1))
79 #define LED2_OFF (gpio_write_pin_output(ledPins[2].pinName, 1))
80 #define LED3_OFF (gpio_write_pin_output(ledPins[3].pinName, 1))
82 #define LED0_ON (gpio_write_pin_output(ledPins[0].pinName, 0))
83 #define LED1_ON (gpio_write_pin_output(ledPins[1].pinName, 0))
84 #define LED2_ON (gpio_write_pin_output(ledPins[2].pinName, 0))
85 #define LED3_ON (gpio_write_pin_output(ledPins[3].pinName, 0))
86 #endif
87 
88 /* MCG Mode defines */
89 #define BLPI 1
90 #define FBI 2
91 #define FEI 3
92 #define FEE 4
93 #define FBE 5
94 #define BLPE 6
95 #define PBE 7
96 #define PEE 8
99 #define UNDEF_VALUE 0xFF
100 
101 #ifdef ON
102 #undef ON
103 #endif
104 #define ON (1)
105 
106 #ifdef OFF
107 #undef OFF
108 #endif
109 #define OFF (0)
110 
111 /* LLWU specific macros */
112 #define LLWU_PIN_DIS 0
113 #define LLWU_PIN_RISING 1
114 #define LLWU_PIN_FALLING 2
115 #define LLWU_PIN_ANY 3
117 /* LP Tiemr specific macros */
118 #define LPTMR_USE_IRCLK 0
119 #define LPTMR_USE_LPOCLK 1
120 #define LPTMR_USE_ERCLK32 2
121 #define LPTMR_USE_OSCERCLK 3
123 /* Clock specific macros */
124 #if (defined(TWR_K22F120M) || defined(FRDM_K22F120M))
125 #define CLK0_FREQ_HZ 8000000
126 #elif (defined(FRDM_K64F120M) || defined(TWR_K64F120M))
127 #define CLK0_FREQ_HZ 50000000
128 #endif
129 
130 #define CRYSTAL 1
131 #define CANNED_OSC 0
133 /* IRC defines */
134 #define SLOW_IRC 0
135 #define FAST_IRC 1
137 #define MCG_OUT_FREQ 48000000
139 #if defined(CPU_MK22FN512VDC12)
140 #define OSC0_CR OSC_CR
141 #define SMC_STOPCTRL_VLLSM_MASK SMC_STOPCTRL_LLSM_MASK
142 #endif
143 
144 #if defined(CPU_MKL25Z128VLK4)
145 #define sysinit SystemInit
146 #endif
147 
148 /*
149  * The expected PLL output frequency is:
150  * PLL out = ((CLKIN/PRDIV) x VDIV)
151  * where the CLKIN is CLK0_FREQ_HZ.
152  *
153  */
154 
155 #define PLL0_PRDIV 4
156 #define PLL0_VDIV 24
158 /*******************************************************************************
159  * Prototypes
160  ******************************************************************************/
161 void clock_monitor(uint8_t enable);
162 void init_hardware(void);
163 void low_power_modes_test(void);
164 void llwu_init(void);
165 void led_init(void);
166 
167 int printf_demo(const char *format,...);
168 void demo_lptmr_isr(void);
169 void port_isr(void);
170 void port_init(void);
171 void set_for_lp(uint8_t outOfLp);
172 int32_t pee_to_blpe(void);
173 int32_t blpe_to_pee(void);
174 void jtag_tdo_pullup_enable(void);
175 void vlp_clock_config(int8_t nextMode);
176 int32_t fll_freq(int32_t fllRef);
177 uint8_t what_mcg_mode(void);
178 void llwu_configure(uint32_t pinEn, uint8_t riseFall, uint8_t moduleEn );
179 void demo_lptmr_isr(void);
180 void lptmr_init(uint32_t count, uint32_t clockSource);
181 
182 void exit_vlpr(void);
183 void enter_stop(smc_stop_submode_t partialStopOpt);
184 int32_t enter_vlpr(void);
185 void enter_wait(power_modes_t pMode);
186 void enter_vlls0(uint8_t PORPOValue);
187 void enter_vlls1(void);
188 void enter_vlls2(void);
189 void enter_vlls3(void);
190 void enter_vlps(void);
191 void enter_lls(void);
192 void vlp_clock_config(int8_t nextMode);
193 uint8_t atc(uint8_t ircSelect, int32_t ircFreq, int32_t mcgOutFreq);
194 
195 int32_t fee_fei(int32_t slowIrcFreqInner);
196 int32_t fei_fbe(int32_t crystalVal, mcg_hgo_select_t hgoVal, mcg_eref_clock_select_t erefsVal);
197 int32_t fbe_fei(int32_t slowIrcFreq);
198 int32_t fei_fbi(int32_t ircFreq, uint8_t ircSelect);
199 int32_t fbi_fei(int32_t slowIrcFreqInner);
200 int32_t fbe_pbe(int32_t crystalVal, int8_t prDivVal, int8_t vDivVal);
201 int32_t pbe_pee(int32_t crystalVal);
202 int32_t pee_pbe(int32_t crystalVal);
203 int32_t pbe_fbe(int32_t crystalVal);
204 int32_t fbe_fbi(int32_t ircFreq, uint8_t ircSelect);
205 int32_t fbi_fbe(int32_t crystalVal, mcg_hgo_select_t hgoVal, mcg_eref_clock_select_t erefsVal);
206 int32_t fbi_fee(int32_t crystalVal, mcg_hgo_select_t hgoVal, mcg_eref_clock_select_t erefsVal);
207 int32_t fbe_fee(int32_t crystalVal);
208 int32_t fee_fbe(int32_t crystalVal);
209 int32_t pbe_blpe(int32_t crystalVal);
210 int32_t blpe_pbe(int32_t crystalVal, int8_t prDivVal, int8_t vDivVal);
211 int32_t blpe_fbe(int32_t crystalVal);
212 int32_t fbi_blpi(int32_t ircFreq, uint8_t ircSelect);
213 int32_t blpi_fbi(int32_t ircFreq, uint8_t ircSelect);
214 int32_t fei_fee(int32_t crystalVal, mcg_hgo_select_t hgoVal, mcg_eref_clock_select_t erefsVal);
215 int32_t fee_fbi(int32_t ircFreq, uint8_t ircSelect);
216 int32_t fbe_blpe(int32_t crystalVal);
217 int32_t pll_init(int32_t crystalVal, mcg_hgo_select_t hgoVal, mcg_eref_clock_select_t erefsVal, int8_t prDivVal, int8_t vDivVal, uint8_t mcgOutSelect);
218 void out_srs(void);
219 void get_uart_data(uart_state_t * uartState, uint8_t * rxBuffer, uint32_t requestedByteCount);
220 
221 #endif /* __LOW_POWER_DEMO_H__*/
222 
223 /*******************************************************************************
224  * EOF
225  ******************************************************************************/
int32_t fbi_fbe(int32_t crystalVal, mcg_hgo_select_t hgoVal, mcg_eref_clock_select_t erefsVal)
switches from FBI to FBE mode
Definition: low_power_demo.c:2685
int32_t pbe_fbe(int32_t crystalVal)
switches from PBE to FBE mode
Definition: low_power_demo.c:2094
void get_uart_data(uart_state_t *uartState, uint8_t *rxBuffer, uint32_t requestedByteCount)
void demo_lptmr_isr(void)
ISR Routine for Low Power Timer.
Definition: low_power_demo.c:1221
void enter_vlls1(void)
VLLS1 mode entry routine. Puts the processor into VLLS1 mode from normal run mode or VLPR...
Definition: low_power_demo.c:594
int32_t fbe_fei(int32_t slowIrcFreq)
int32_t pee_pbe(int32_t crystalVal)
switches from PEE to PBE mode
Definition: low_power_demo.c:1993
void vlp_clock_config(int8_t nextMode)
clock configuration for very low power
Definition: low_power_demo.c:874
void llwu_configure(uint32_t pinEn, uint8_t riseFall, uint8_t moduleEn)
Set up the LLWU for wakeup the MCU from LLS and VLLSx modes from the selected pin or module...
Definition: llwu_MK22F51212.c:69
int32_t fbe_fbi(int32_t ircFreq, uint8_t ircSelect)
switches from FBE to PLPE mode
Definition: low_power_demo.c:2564
int32_t fee_fbi(int32_t ircFreq, uint8_t ircSelect)
switches from FEE to FBI mode
Definition: low_power_demo.c:2894
int32_t fee_fbe(int32_t crystalVal)
switches from FEE to FBE mode
Definition: low_power_demo.c:2526
int32_t fbe_fee(int32_t crystalVal)
int32_t blpe_to_pee(void)
switch from BLPE mode to PEE mode
Definition: low_power_demo.c:395
void lptmr_init(uint32_t count, uint32_t clockSource)
Low Power Timer initialization routine.
Definition: low_power_demo.c:1247
static void init_hardware(void)
Init hardware for TWR-K70F120M.
Definition: flexcan_demo/src/main.c:95
void enter_vlls3(void)
enter in VLLS3 mode
Definition: low_power_demo.c:653
int32_t fee_fei(int32_t slowIrcFreqInner)
switches from
Definition: low_power_demo.c:3306
int32_t fll_freq(int32_t fllRef)
returns the Frequency Locked Loop frequency
Definition: low_power_demo.c:224
void out_srs(void)
checks the value in the SRS registers and sends messages to the terminal announcing the status at the...
Definition: low_power_demo.c:3713
int32_t fbe_pbe(int32_t crystalVal, int8_t prDivVal, int8_t vDivVal)
switches from FBE to PBE mode This function transitions the MCG from FBE mode to PBE mode...
Definition: low_power_demo.c:2144
int32_t pbe_pee(int32_t crystalVal)
switches from PBE to PEE mode
Definition: low_power_demo.c:2034
void enter_vlps(void)
enter in VLPS mode
Definition: low_power_demo.c:639
int32_t fei_fee(int32_t crystalVal, mcg_hgo_select_t hgoVal, mcg_eref_clock_select_t erefsVal)
switches from FEI to FEE mode
Definition: low_power_demo.c:3178
int32_t fbe_blpe(int32_t crystalVal)
switches from FBE to PLPE mode
Definition: low_power_demo.c:2363
int32_t enter_vlpr(void)
VLPR mode entry routine.Puts the processor into very low power run mode. In this mode all clocks are ...
Definition: low_power_demo.c:750
int32_t blpe_pbe(int32_t crystalVal, int8_t prDivVal, int8_t vDivVal)
switches from BLPE to PBE mode Since PBE mode can be enterred via FBE -&gt; BLPE modes, it cannot be assumed that the PLL has been previously configured correctly. That is why this general purpose driver has the PLL settings as passed parameters.
Definition: low_power_demo.c:2251
int32_t pee_to_blpe(void)
switch from PEE mode to BLPE mode
Definition: low_power_demo.c:380
void port_isr(void)
Port ISR routine for the on board switch.
Definition: low_power_demo.c:3792
int32_t pbe_blpe(int32_t crystalVal)
switches from PBE to BLPE mode
Definition: low_power_demo.c:2221
uint8_t atc(uint8_t ircSelect, int32_t ircFreq, int32_t mcgOutFreq)
configure for MCG auto trimming
Definition: low_power_demo.c:3363
int32_t fbi_fee(int32_t crystalVal, mcg_hgo_select_t hgoVal, mcg_eref_clock_select_t erefsVal)
int32_t blpi_fbi(int32_t ircFreq, uint8_t ircSelect)
switches from BLPI to FBI mode
Definition: low_power_demo.c:2861
int32_t fei_fbi(int32_t ircFreq, uint8_t ircSelect)
switches from FEI to FBI mode
Definition: low_power_demo.c:3071
void clock_monitor(uint8_t enable)
int32_t fei_fbe(int32_t crystalVal, mcg_hgo_select_t hgoVal, mcg_eref_clock_select_t erefsVal)
switches from FEI to FBE mode
Definition: low_power_demo.c:2399
static uart_state_t uartState
Definition: low_power_demo.c:66
uint8_t what_mcg_mode(void)
returns the the current MCG mode
Definition: low_power_demo.c:300
int32_t slowIrcFreq
Definition: low_power_demo.c:70
void set_for_lp(uint8_t outOfLp)
Settings done before swithing in low power mode.
Definition: low_power_demo.c:416
static sai_data_format_t * format
Definition: modulator.c:60
void low_power_modes_test(void)
main low power mode test routine
Definition: low_power_demo.c:1316
void enter_lls(void)
LLS mode entry routine. Puts the processor into LLS mode from normal run mode or VLPR.
Definition: low_power_demo.c:681
void enter_stop(smc_stop_submode_t partialStopOpt)
STOP mode entry routine. Puts the processor into normal stop mode. In this mode core, bus and peripheral clocks are disabled.
Definition: low_power_demo.c:859
void llwu_init(void)
initializes LLWU module
Definition: low_power_demo.c:1290
void jtag_tdo_pullup_enable(void)
int32_t blpe_fbe(int32_t crystalVal)
switches from BLPE to FBE mode
Definition: low_power_demo.c:2326
void led_init(void)
configure LED pins for the tower/freedom hardware
Definition: low_power_demo.c:209
int32_t fbi_blpi(int32_t ircFreq, uint8_t ircSelect)
switches from FBI to BLPI mode This function transitions the MCG from FBI mode to BLPI mode...
Definition: low_power_demo.c:2819
void port_init(void)
configure tower/freedom hardware port pins for the application
Definition: low_power_demo.c:140
void enter_vlls0(uint8_t PORPOValue)
VLLS0 mode entry routine. Puts the processor into VLLS0 mode from normal run mode or VLPR...
Definition: low_power_demo.c:563
int printf_demo(const char *format,...)
void enter_vlls2(void)
VLLS2 mode entry routine. Puts the processor into VLLS2 mode from normal run mode or VLPR...
Definition: low_power_demo.c:624
void enter_wait(power_modes_t pMode)
WAIT mode entry routine. Puts the processor into wait mode. In this mode the core clock is disabled (...
Definition: low_power_demo.c:827
int32_t fbi_fei(int32_t slowIrcFreqInner)
switches from FBI to FEI mode
Definition: low_power_demo.c:3006
void exit_vlpr(void)
VLPR mode exit routine. Puts the processor into normal run mode from VLPR mode. You can transition fr...
Definition: low_power_demo.c:701
int32_t pll_init(int32_t crystalVal, mcg_hgo_select_t hgoVal, mcg_eref_clock_select_t erefsVal, int8_t prDivVal, int8_t vDivVal, uint8_t mcgOutSelect)
Option to move from FEI to PEE mode or to just initialize the PLL.
Definition: low_power_demo.c:3506